<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2017/1/17
 * Time: 22:27
 */

namespace console\controllers;

use yii\console\Controller;
use linslin\yii2\curl;
use common\models\Toutiao;

class ToutiaoController extends Controller
{
    public function actionIndex()
    {
        $t = time();
        $e = strtoupper(dechex($t));
        $n = strtoupper(md5($t));
        $o = substr($n, 0, 5);
        $i = substr($n,-5);
        $a = '';
        $l = "";
        $as = "";
        $cp = "";
        for($r = 0; 5 > $r; $r++) {
            $a .= $o[$r].$e[$r];
        }
        for($s = 0; 5 > $s; $s++) {
            $l .= $e[$s + 3].$i[$s];
        }
        $as = "A1".$a.substr($e, -3);
        $cp = substr($e,0,3).$l."E1";
        $curl = new curl\Curl();
        $url = 'http://www.toutiao.com/api/pc/feed/?category=news_regimen&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&as='.$as.'&cp='.$cp;
        $response = $curl->setOptions(array(CURLOPT_COOKIE => 'tt_webid=51459361946;utm_source=toutiao;__utmc=24953151'))->get($url);
        $response = json_decode($response, true);
        if ($response['message'] == 'success') {
            foreach($response['data'] as $data) {
                if (Toutiao::findByGroupId($data['group_id'])) continue;
                if ($data['source'] == '头条问答') continue;
                if ($data['tag'] == 'news_entertainment') continue;
                if ($data['tag'] == 'news_fashion') continue;
                if ($data['tag'] == 'news_food') continue;
                if ($data['tag'] == 'news_agriculture') continue;
                if ($data['tag'] == 'news_pet') continue;
                if ($data['tag'] == 'news_psychology') continue;
                if ($data['tag'] == 'news_sports') continue;
                if ($data['tag'] == 'news_society') continue;
                if ($data['tag'] == 'news_culture') continue;
                if ($data['tag'] == 'funny') continue;
                if ($data['tag'] == 'news_game') continue;
                $model = new Toutiao();
                $model->title           = $data['title'];
                $model->tag_url         = $data['tag_url'];
                $model->chinese_tag     = $data['chinese_tag'];
                $model->source_url      = 'http://www.toutiao.com'.$data['source_url'];
                $model->source          = $data['source'];
                $model->tag             = $data['tag'];
                $model->group_id        = $data['group_id'];
                $model->media_url       = isset($data['media_url']) && $data['media_url'] ? $data['media_url'] : '';
                $model->add_time        = time();
                $model->status          = 1;  // 信息可用
                $model->save(false);
            }
        }
    }
}
